<?php

interface WithdrawStatus{
	// 未转账
	const UNPAY=0;
	// 转账成功
	const PAY_DONE=1;
	// 转账失败
	const PAY_FAIL=2;
}

/**
 * 提现口令长度
 */
define('WITHDRAW_CODE_LENGTH',8);

/**
 *
 * @author Administrator
 *        
 */
class WithdrawOrderModel extends \MY_Model {
	
	public function __construct() {
		$this->_table = 'hs_withdraw_order';
		parent::__construct ( );
	}
	/**
	 * 创建一笔提现订单
	 * @param unknown $uid
	 */
	public function create($uid,$amount){
		// 生成唯一码
		$this->db->trans_begin();
		//生成提现订单
		$order = array(
				'user_id'=>$uid,
				'amount'=>$amount,
				'create_time'=>time(),
				'wx_key'=>'default',
				'status'=>WithdrawStatus::UNUSED,
		);
		$withdraw_id = $this->db->insert($this->_table(),$order);
		
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			return false;
		}else{
			$this->db->trans_commit();
			return $order;
		}	
		
	}
	
	public function getStatusDesc($status){
		$map = array(
				WithdrawStatus::UNUSED =>'未使用',
				WithdrawStatus::UNCHECKUP =>'审核中',
				WithdrawStatus::CHECNUP_PASS =>'审核通过',
				WithdrawStatus::CHECNUP_FAIL =>'审核不通过',
				WithdrawStatus::PAY_DONE =>'转账成功',
				WithdrawStatus::PAY_FAIL =>'转账失败',
		);
		
		return isset($map[$status]) ? $map[$status] : '失败';
		
	}
	
// 	public function checkupPass($orderId){
// 		$data = array(
// 				'status'=>WithdrawStatus::CHECNUP_PASS,
// 				'update_time'=>time(),
// 		);
// 		return $this->updateOne('id', $orderId, $data);
// 	}
	


	/**
	 * 转账成功
	 * @param unknown $orderId
	 * @param unknown $uid
	 * @param unknown $amount
	 * @param unknown $wxPaymentID
	 */
	public function transfersDone($orderId,$uid,$amount,$wxPaymentID,$wxLog=''){
		
		$this->db->trans_begin();
		
		// 更新提现订单表
		$this->db->set('update_time',time())
				 ->set('status',WithdrawStatus::PAY_DONE)
				 ->set('wx_payment_id',$wxPaymentID)
				 ->set('transfer_time',time())
				 ->set('wx_log',$wxLog)
				 ->where('id',$orderId)
				 ->update($this->_table());
		// 更新用户数据

		//$this->db->set('out_revenue','out_revenue + '.$amount,FALSE)
// 		$this->db->set('outTotal','outTotal + '.$amount,FALSE)
// 				 ->set('lock_revenue','lock_revenue - '.$amount,FALSE)
// 				 ->where('id',$uid)
// 				 ->update(parent::T_USER);
		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			return FALSE;
		}else{
			$this->db->trans_commit();
			return TRUE;
		}
	}
	/**
	 * 转账失败
	 * @param unknown $orderId
	 * @param unknown $uid
	 * @param unknown $amount
	 * @param unknown $wxError
	 * @param unknown $failReason
	 */
	public function transfersFail($orderId,$uid,$amount,$wxError,$failReason,$wxLog=''){
		$this->db->trans_begin();
		// 更新提现订单表
		$this->db->set('update_time',time())
				 ->set('status',WithdrawStatus::PAY_FAIL)
				 ->set('wx_error',$wxError)
				 ->set('fail_reason',$failReason)
				 ->set('wx_log',$wxLog)
				 ->where('id',$orderId)
				 ->update($this->_table());
		// 更新用户数据
// 		$this->db->set('lock_revenue','lock_revenue - '.$amount,FALSE)
// 				 ->where('id',$uid)
// 				 ->update(parent::T_USER);

		if ($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();
			return FALSE;
		}else{
			$this->db->trans_commit();
			return TRUE;
		}
	}
	
	
}

